/*****************************************************************************
*Purpose: Estimate binscatter of earnings Children of Gautreaux Participants 
******************************************************************************/ 

**********************************
******** Binscatter Fig **********
**********************************

foreach j in earn24 prearn24 place_incrank  {
	reghdfe `j' if estsample==1 & earn24!=., absorb($fe) residual(r_`j')
	sum `j' if e(sample)==1
	replace r_`j'=r_`j'+r(mean)
}

foreach q in place_shwhite {
	reghdfe `q' if estsample==1 & earn24!=., absorb($fe) residual(r_`q')
}


gen numpiks_earn=estsample if r_earn24!=.
replace prearn24=. if earn24==.






capture frame drop figplot
frame create figplot str35 sourcedata str35 outcome str35 group str35 spec beta se obs piks treatpik controlpik 

binscatter earn24 prearn24 place_shwhite if estsample==1 & earn24!=., absorb(mitrct80) controls(i.intakeyear i.yrbirth) genxq(plotbins)
reghdfe prearn24 place_shwhite if estsample==1 & earn24!=., absorb($fe) cluster(hhid)

unique pik if e(sample)==1
local numpik `=r(sum)'

frame post figplot ("LEHD") ("prearn24") ("all") ("binplot_coef") (_b[place_shwhite]) (_se[place_shwhite]) (`numpik') (`numpik') (-99999) (-99999)
frame post figplot ("LEHD") ("prearn24") ("all") ("binplot_intercept") (_b[_cons]) (_se[_cons]) (`numpik') (`numpik') (-99999) (-99999)


reghdfe earn24 place_shwhite if estsample==1 & earn24!=., absorb($fe) cluster(hhid)
unique pik if e(sample)==1
local numpik `=r(sum)'

frame post figplot ("LEHD") ("earn24") ("all") ("binplot_coef") (_b[place_shwhite]) (_se[place_shwhite]) (`numpik') (`numpik') (-99999) (-99999)
frame post figplot ("LEHD") ("earn24") ("all") ("binplot_intercept") (_b[_cons]) (_se[_cons]) (`numpik') (`numpik') (-99999) (-99999)


******************

preserve
keep if plotbins!=.
collapse (mean) r_earn24 r_prearn24  r_place_shwhite (max) estsample (sum) numpiks_earn , by(plotbins)


foreach w in r_earn24 r_prearn24  r_place_shwhite  {
forvalues j=1/18 {
	if "`w'"=="r_earn24" | "`w'"=="r_prearn24"   {
	sum numpiks_earn if plotbins==`j'
	local numpik `=r(mean)'
	local source "LEHD"
	}


	sum `w' if plotbins==`j' 
	frame post figplot ("`source'") ("`w'") ("quantile`j'") ("binplot") (r(mean)) (-99999) (`numpik') (`numpik') (-99999) (-99999)
	
}
}

frame figplot: save "${output}/figplot_results.dta", replace
frame figplot: export delimited using "${output}/figplot_results.csv", replace
restore 

******************************************
**** Binscatter with Opp Altas metric ****
******************************************

capture frame drop figplot2
frame create figplot2 str35 sourcedata str35 outcome str35 group str35 spec beta se obs piks treatpik controlpik 

capture drop oiplotbins
binscatter earn24 prearn24 place_incrank if estsample==1 & earn24!=., absorb(mitrct80) controls(i.intakeyear i.yrbirth) genxq(oiplotbins)

reghdfe prearn24 place_incrank if estsample==1 & earn24!=., absorb($fe) cluster(hhid)

unique pik if e(sample)==1
local numpik `=r(sum)'

frame post figplot2 ("LEHD") ("prearn24") ("all") ("binplot_coef") (_b[place_incrank]) (_se[place_incrank]) (`numpik') (`numpik') (-99999) (-99999)
frame post figplot2 ("LEHD") ("prearn24") ("all") ("binplot_intercept") (_b[_cons]) (_se[_cons]) (`numpik') (`numpik') (-99999) (-99999)


reghdfe earn24 place_incrank if estsample==1 & earn24!=., absorb($fe) cluster(hhid)
unique pik if e(sample)==1
local numpik `=r(sum)'

frame post figplot2 ("LEHD") ("earn24") ("all") ("binplot_coef") (_b[place_incrank]) (_se[place_incrank]) (`numpik') (`numpik') (-99999) (-99999)
frame post figplot2 ("LEHD") ("earn24") ("all") ("binplot_intercept") (_b[_cons]) (_se[_cons]) (`numpik') (`numpik') (-99999) (-99999)

******************

preserve
keep if oiplotbins!=.
collapse (mean) r_earn24 r_prearn24 r_place_incrank (max) estsample (sum) numpiks_earn , by(oiplotbins)


foreach w in r_earn24 r_prearn24 r_place_incrank  {
forvalues j=1/18 {
	if "`w'"=="r_earn24" | "`w'"=="r_prearn24"  {
	sum numpiks_earn if oiplotbins==`j'
	local numpik `=r(mean)'
	local source "LEHD"
	}


	sum `w' if oiplotbins==`j' 
	frame post figplot2 ("`source'") ("`w'") ("quantile`j'") ("binplot") (r(mean)) (-99999) (`numpik') (`numpik') (-99999) (-99999)
	
}
}

frame figplot2: save "${output}/oifigplot_results.dta", replace
frame figplot2: export delimited using "${output}/oifigplot_results.csv", replace
restore 
******


